import sys, os, re
import datetime

sub_file = 'sub_test.txt'

def parse_sub(sub):
    result = ''
    lines = re.compile('{"s":"(.+?)",.*?"f":(.+?),.+?"t":(.+?)}').findall(sub)
    index = 1
    for s, f, t in lines:
        dt1 = datetime.datetime(1970,1,1) + datetime.timedelta(seconds = int(f))
        dt2 = datetime.datetime(1970,1,1) + datetime.timedelta(seconds = int(t))
        result = result + str(index) + '\n' + dt1.strftime('%H:%M:%S,100') + ' --> ' + dt2.strftime('%H:%M:%S,000') + '\n'
        for ln in s.split('<br>'):
            result = result + ln + '\n'
        result = result + '\n'
        index = index + 1
    return result

f = open(sub_file)
old_sub = f.read()
f.close()

new_sub = parse_sub(old_sub)

f2 = open('sub_test.srt','w')
f2.write(new_sub)
f2.close()

print 'Done.'
